import MineFetch from '../../../utils/service/Mine';
import HomeService from 'utils/service/HomeService.js';
import MyOrder from '../../../utils/service/MyOrder';
import { getDate } from '../../../utils/util';

const homeService = new HomeService();
const mineFeth = new MineFetch();
const myOrder = new MyOrder();
const app = getApp();
const orderMenus = [
  {
    name: '全部订单',
    status: 'ALL',
    count: 0,
    icon: '/images/others/all-order.png'
  },
  {
    name: '待接单',
    status: 'CREATED',
    count: 0,
    icon: '/images/others/waiting-receipt.png'
  },
  {
    name: '待发货',
    status: 'CUTTED',
    count: 0,
    icon: '/images/others/received.png'
  },
  {
    name: '待收货',
    status: 'STOCKOUT',
    count: 0,
    icon: '/images/others/waiting-goods.png'
  },
  {
    name: '已签收',
    status: 'HOME',
    count: 0,
    icon: '/images/others/has-finished.png'
  }
  // {
  //   name: '已取消',
  //   status: 'CANCELLED',
  //   count: 0,
  //   icon: '/images/others/has-canceled.png'
  // }
];
Page({
  /**
   * 页面的初始数据
   */
  data: {
    credit: '',
    sapCusName: '', // 客户名称
    sapSubCusCode: '', // 子客户编码
    userInfo: {}, // 存储 用户信息
    orderMenus,
    order: null,
    defaultImg: app.globalData.rectImg
  },
  onShow() {
    const userInfo = wx.getStorageSync('userInfo');
    this.setData({
      userInfo
    });
    const param = {
      subCusCode: userInfo.yszxSapSubCusCode
    };
    myOrder
      .getOrderListAll({
        page: 1,
        size: 1,
        orderStatus: 'ALL'
      })
      .then(({ result }) => {
        const order = result.result && result.result[0];
        if (order) {
          order.orderTime = getDate(order.orderTime);
        }
        this.setData({ order: order || null });
      });
    // 信控余额
    mineFeth.getCredit(param).then((data) => {
      this.setData({
        credit: data.result
      });
    });
    // 各种状态订单数量
    mineFeth.statusCount().then((data) => {
      let allCount = 0;
      const { result } = data;
      orderMenus.forEach((item) => {
        result.forEach((r) => {
          if (item.status === r.status) {
            item.count = r.count;
            allCount += r.count;
          }
        });
      });
      orderMenus.find(item => item.status === 'ALL').count = allCount;
      this.setData({
        orderMenus
      });
    });

    // 子客户名称
    homeService.getSubList(userInfo.id).then(({ result }) => {
      const defaultUser = result.filter(item => item.currentCusFlg === 1); // 默认子客户号
      this.setData({
        sapCusName: defaultUser[0].sapSubCusName,
        sapSubCusCode: defaultUser[0].sapSubCusCode
      });
    });
  },
  onClickToAllOrder() {
    wx.navigateTo({
      url: '/pages/mine/order/order?status=ALL'
    });
  },
  onClickMoreOrder(e) {
    const status = e.currentTarget.dataset.status || 'ALL';
    wx.navigateTo({
      url: `/pages/mine/order/order?status=${status}`
    });
  },

  handleToTable() {
    wx.navigateTo({
      url: '/pages/table/index/index'
    });
  },
  viewOrder() {
    const { order } = this.data;
    wx.navigateTo({
      url: `/pages/mine/order-detail/order-detail?orderNo=${order.orderNo}`
    });
  },

  onShareAppMessage() {
    return {
      title: '企业购-我的',
      path: '/pages/home/index/home'
    };
  }
});
